<?php
namespace Home\Controller;
use Think\Controller;
class BlogController extends Controller {
    //blog首页
    public function index($month="",$tag="",$keyword="",$currentPage = 1){
        $pageSize = 10;
        $Form = M("blog");
        //获取笔记每月统计（缓存一天）
        if(!S("monthSession")){
            $Model = new \Think\Model();
            $temp = $Model->query("select date_format(create_time,'%y年%m月') monthstext,date_format(create_time,'%Y%m') months,count(id) count from blog group by months ORDER BY months desc");
            S("monthSession",$temp,86400);
        }
        //获取笔记标签统计(缓存一天)
        if(!S("tagCountSession")){
            $Model = new \Think\Model();
            $tempList = $Model->query('select count(1) count from blog where tag like "%js%"');
            $count[0]= $tempList[0];
            $tempList = $Model->query('select count(1) count from blog where tag like "%css%"');
            $count[1] = $tempList[0];
            $tempList = $Model->query('select count(1) count from blog where tag like "%html%"');
            $count[2] = $tempList[0];
            $tempList = $Model->query('select count(1) count from blog where tag like "%plugin%"');
            $count[3] = $tempList[0];
            S("tagCountSession",$count,86400);
        }
        //初始化查询条件(未删除的数据)
        $where['delete'] = 0;
        //是否输入关键词
        if($keyword != ""){
            $where['_string'] = 'title like "%'.$keyword.'%" or abstract like "%'.$keyword.'%"';
            $this->assign('keyword',$keyword);
        }
        //是否从tag进入
        if($tag != ""){
            $where['_string'] = 'tag like "%'.$tag.'%"';
            $this->assign('tag',$tag);
        }
        //是否从月份进入
        if($month != ""){
            $where['_string'] = 'date_format(create_time,"%Y%m") = "'.$month.'"';
            $this->assign('month',$month);
        }
        //查询数据
        $total = $Form->where($where)->count();
        $data = $Form->where($where)->order('create_time desc')->limit($pageSize*($currentPage - 1),$pageSize)->select();
        $totalPage = $total/$pageSize;
        if($total % $pageSize != 0) $totalPage++;
        $this->assign('monthSession',S("monthSession"));
        $this->assign('tagCountSession',S("tagCountSession"));
        $this->assign('total',$total);
        $this->assign('currentPage',$currentPage);
        $this->assign('totalPage',$totalPage);
        $this->assign('data',$data);
        $this->assign('URL_PREFIX',C("URL_PREFIX"));
        $this->display(T('Index/Blog/index'));
    }
    //blog详情
    public function detail($id){
        $Form = M("blog");
        $data = $Form -> where('id ='.$id)->find();
        $this->assign('data',$data);
        $this->assign('URL_PREFIX',C("URL_PREFIX"));
        $this->display(T('Index/Blog/detail'));
    }
}